package cn.edu.gxnu.dsa.lab;

import cn.edu.gxnu.dsa.queue.CircleSequenceQueue;

public class TestFerrySim {
	private CircleSequenceQueue q = new CircleSequenceQueue(11);
	private CircleSequenceQueue b = new CircleSequenceQueue(100);
	private CircleSequenceQueue t = new CircleSequenceQueue(100);
	private String carSequence;

	public TestFerrySim(String carSequence) {
		processSequence(carSequence);
	}
	
	public void start(){
		ferryManagment();
	}

	private void processSequence(String carSequence) {
		this.carSequence = carSequence;
		char vehicleType;
		for (int i = 0; i < this.carSequence.length(); i++) {
			vehicleType = this.carSequence.charAt(i);
			if (vehicleType == ' ') {
				continue;
			} else {
				switch(vehicleType){
				case 'B':
					b.enQueue('B');
					break;
				case 'T':
					t.enQueue('T');
					break;
				}
			}
		}

	}

	private void ferryManagment() {
		int i = 0;
		int j = 0;
		while (j < 10) {
			if (!b.isEmpty() && i < 4) {
				q.enQueue(b.deQueue());
				i++;
				j++;
			} else if (i == 4 && !t.isEmpty()) {
				q.enQueue(t.deQueue());
				j++;
				i = 0;
			} else {
				while (j < 10 && i < 5 && !t.isEmpty()) {
					q.enQueue(t.deQueue());
					i++;
					j++;
				}
				i = 0;
			}
			if(j==10){
				q.display();
				System.out.println();
				q.clear();
				j=0;
			}
			if(b.isEmpty()&&t.isEmpty()){
				j=10;
				q.display();
			}
		}
	}

}
